# -!- coding: utf-8 -!-
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#作者：cacho_37967865
#博客：https://blog.csdn.net/sinat_37967865
#文件：get_stock_add.py
#日期：2021-01-16
#备注：获取东方财富网的定增股票信息   http://data.eastmoney.com/notices/hsa/2.html
http://data.eastmoney.com/notices/getdata.ashx?StockCode=300470&FirstNodeType=2&CodeType=A&PageIndex=1&PageSize=50
http://data.eastmoney.com/notices/getdata.ashx?FirstNodeType=2&CodeType=A&PageIndex=1&PageSize=50
drop table if exists stock_notices;
CREATE TABLE `stock_notices` (
  `stock_no` varchar(32) NOT NULL COMMENT '股票代码',
  `stock_name` varchar(32) NOT NULL COMMENT '股票简称',
  `notice_date` varchar(32) NOT NULL COMMENT '公告日期',
  `notice_type` varchar(32) NOT NULL COMMENT '公告类型',
  `notice_title` varchar(200) NOT NULL COMMENT '公告名称',   
  `process` int(3) NOT NULL COMMENT '流程：1-增发预案，2-审核通过，3-核准批复，4-发行成功',
  `price_type` int(3) NOT NULL DEFAULT '0' COMMENT '定价规则：0-竞价，1-定价',
  `price` float NOT NULL DEFAULT '0' COMMENT '定增价格',   
  `price_md` float NOT NULL DEFAULT '0' COMMENT '股本改变后的定增价格',   
  `create_time` datetime NOT NULL COMMENT '创建时间',
  PRIMARY KEY (`stock_no`,`process`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='沪深股票定增公告信息';
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

import requests
from pacong.stock.stock_config import db,cursor,headers
from pycacho.cachobase.logger import Logger

logger = Logger("get_stock_add").get_log()


# 获取
def stock_add_notice(stock_code,n):
    url = 'http://np-anotice-stock.eastmoney.com/api/security/ann?page_size=50&ann_type=A&f_node=5' \
          '&stock_list={0}&page_index={1}'.format(stock_code,n)
    print(url)
    resp = requests.get(url, headers=headers)
    data = resp.text.replace('var  = ','').replace('};','}').replace('null','None')
    json_data = eval(data)['data']
    #print(json_data)
    for info in json_data:
        stock_no = info['codes'][0]['stock_code']
        stock_name = info['codes'][0]['short_name']
        notice_date = info['notice_date'][0:10]
        notice_type = info['columns'][0]['column_name']   # # 增发预案、增发获准公告、增发终止
        notice_title = info['title']
        if notice_type=='增发预案'and '股票预案' in notice_title and '提示性公告' not in notice_title :
            process = 1
        elif notice_type=='增发获准公告' and '审核通过的公告' in notice_title:
            process = 2
        elif notice_type=='增发获准公告' and ('核准' in notice_title or '注册批复' in notice_title):
            process = 3
        elif (notice_type == '其他增发事项公告' and '发行情况报告书' in notice_title) or (notice_type == '增发上市公告书' and '发行情况及上市公告书' in notice_title) :
            process = 4
        elif notice_type == '增发终止' and '非公开发行' in notice_title:
            process = 0
        else:
            process = 100
        if process != 100:
            print(stock_no, stock_name, notice_date, notice_type, notice_title, process)
            notice_to_mysql(stock_no, stock_name, notice_date, notice_type, notice_title, process)

# 沪深重要指数
def notice_to_mysql(stock_no, stock_name, notice_date, notice_type, notice_title, process):
    sql = "insert into stock_notices (stock_no, stock_name, notice_date, notice_type, notice_title, process,price_type,price,price_md,create_time,remark) " \
          "values ('%s','%s','%s','%s','%s','%s','竞价',0,0,now(),'');" \
          % (stock_no, stock_name, notice_date, notice_type, notice_title, process)
    try:
        cursor.execute(sql)
    except Exception as e:
        # 发生错误时回滚
        db.rollback()
        print(str(e))
    else:
        db.commit()
        #logger.info('事务处理成功')


if __name__ == '__main__':
    for i in range(1,2):
        stock_add_notice('',i)





